/*
 * ====================================================================
 *
 * ALL RIGHTS RESERVED.
 * COPYRIGHT(C)2019,DHEE.
 */

package comm.dhee.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;

import comm.dhee.business.PR101Business;
import comm.dhee.business.bean.LogonForm;
import comm.dhee.business.bean.PR10101From;
import comm.dhee.business.impl.PR101BusinessImpl;
import comm.dhee.dto.M001;
import comm.dhee.dto.M002;
import comm.dhee.dto.M002T002;
import comm.dhee.util.Common;
import comm.dhee.util.ExcelUtils;

/**
 * PR101受注入力に関するデータアクセスインタフェース。
 * 
 * @author 張剣南
 * @version $Revision: 1 $
 * 
 *          <pre>
 * ■変更履歴■
 * 2019/03/16 新規作成
 *          </pre>
 */
@Controller
public class PR101Controller {

	List<M002> zhuijiasholist = new ArrayList<M002>();

	/**
	 * PR10101初期画面へ遷移する。
	 * 
	 * @throws SQLException
	 * @return String PR10101
	 */
	@RequestMapping("/toPR10101.action")
	public String toPR10101(HttpServletRequest request) {
		zhuijiasholist.clear();
		Common common = new Common();
		PR101Business pr101Business = new PR101BusinessImpl();

		List<M001> kokyakulist = pr101Business.kokyakulist("");
		for (int i = 0; i < kokyakulist.size(); i++) {
			kokyakulist.get(i).setStrshinyouGendoGaku(common.zifuchuan(kokyakulist.get(i).getStrshinyouGendoGaku()));
		}
		// 顧客リストを設定します
		request.setAttribute("kokyakulist", kokyakulist);

		List<M002> shouhinlist = pr101Business.shouhinlist("");
		for (int i = 0; i < shouhinlist.size(); i++) {
			shouhinlist.get(i).setStruriageTanka(common.zifuchuan(shouhinlist.get(i).getStruriageTanka()));
			shouhinlist.get(i).setStrzaikoSuu(common.zifuchuan(shouhinlist.get(i).getStrzaikoSuu()));
		}
		// 商品 リストを設定します
		request.setAttribute("shouhinlist", shouhinlist);
		SimpleDateFormat dfFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date sysdate = new Date();
		String stsysdate = dfFormat.format(pr101Business.getNextday(sysdate));

		request.setAttribute("stsysdate", stsysdate);

		return "PR10101";
	}

	/**
	 * selkokyakuList.action画面へ遷移する。
	 * 
	 * @throws SQLException
	 * @return Map<String, Object> map
	 */
	@RequestMapping("/selkokyakuList.action")
	@ResponseBody
	public Map<String, Object> selkokyakuList(HttpServletRequest request) {
		Common common = new Common();
		// 顧客名を取得する
		String kokyakuMei = request.getParameter("kokyakuMei");

		Map<String, Object> map = new HashMap<String, Object>();
		PR101Business pr101Business = new PR101BusinessImpl();
		// 顧客 リストの初期化
		List<M001> kokyakulist = new ArrayList<M001>();
		// 顧客 リストを設定する
		kokyakulist = pr101Business.kokyakulist(kokyakuMei);
		for (int i = 0; i < kokyakulist.size(); i++) {
			kokyakulist.get(i).setStrshinyouGendoGaku(common.zifuchuan(kokyakulist.get(i).getStrshinyouGendoGaku()));
		}
		map.put("kokyakulist", kokyakulist);

		return map;
	}

	/**
	 * selshouhinList.action画面へ遷移する。
	 * 
	 * @throws SQLException
	 * @return Map<String, Object> map
	 */
	@RequestMapping("/selshouhinList.action")
	@ResponseBody
	public Map<String, Object> selshouhinList(HttpServletRequest request) {
		Common common = new Common();
		// 商品名を取得する
		String shouhinMei = request.getParameter("shouhinMei");

		Map<String, Object> map = new HashMap<String, Object>();
		PR101Business pr101Business = new PR101BusinessImpl();
		// 商品 リストの初期化
		List<M002> shouhinlist = new ArrayList<M002>();
		// 商品 リストを設定する
		shouhinlist = pr101Business.shouhinlist(shouhinMei);

		for (int i = 0; i < shouhinlist.size(); i++) {
			shouhinlist.get(i).setStruriageTanka(common.zifuchuan(shouhinlist.get(i).getStruriageTanka()));
			shouhinlist.get(i).setStrzaikoSuu(common.zifuchuan(shouhinlist.get(i).getStrzaikoSuu()));
		}
		map.put("shouhinlist", shouhinlist);

		return map;
	}

	/**
	 * selShouhin.action画面へ遷移する。
	 * 
	 * @throws SQLException
	 * @return Map<String, Object> map
	 */
	@RequestMapping("/selShouhin.action")
	@ResponseBody
	public Map<String, Object> selshouhin(HttpServletRequest request) {
		Common common = new Common();
		//前台商品コードを取得する
		String shouhinCode = request.getParameter("shouhinCode");
		//在庫数を取得する
		String uriageSuu = request.getParameter("uriageSuu");
		//Map初期化
		Map<String, Object> map = new HashMap<String, Object>();

		PR101Business pr101Business = new PR101BusinessImpl();
		M002 m002 = new M002();

		m002 = pr101Business.selshouhin(shouhinCode);
		m002.setStruriageTanka(common.zifuchuan(m002.getStruriageTanka()));
		m002.setStrzaikoSuu(common.zifuchuan(m002.getStrzaikoSuu()));
		int inturiageSuu = Integer.parseInt(uriageSuu);
		//在庫商品データを取得する
		if (inturiageSuu > m002.getZaikoSuu()) {
			map.put("result", "zaikoSuu");
			return map;
		} else {
			m002.setUriageSuu(Integer.parseInt(uriageSuu));
			map.put("m002", m002);
			zhuijiasholist.add(m002);
			map.put("uriageSuu", uriageSuu);
			return map;
		}

	}

	/**
	 * faxing.action画面へ遷移する。
	 * 
	 * @throws SQLException
	 * @return String PR10101
	 */
	@RequestMapping("/faxing.action")
	public String faxing(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PR101Business pr101Business = new PR101BusinessImpl();
		Common common = new Common();
		SimpleDateFormat dfFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date sysdate = new Date();
		//前台顧客コードを取得する
		String gukecode = request.getParameter("gukecode");
		//前台納入希望日を取得する
		String narudate = request.getParameter("narudate");
		//前台納入先を取得する
		String naruxian = request.getParameter("naruxian");
		//前台boxを取得する
		String[] box = request.getParameterValues("box");

		if (box == null) {
			request.setAttribute("msg", "alert('" + Common.M101004E + "');");
			request.setAttribute("kokyakulist", pr101Business.kokyakulist(""));
			request.setAttribute("shouhinlist", pr101Business.shouhinlist(""));
			String stsysdate = dfFormat.format(pr101Business.getNextday(sysdate));
			request.setAttribute("zhuijiasholist", zhuijiasholist);
			request.setAttribute("stsysdate", stsysdate);
			request.setAttribute("gukecode", gukecode);
			return "PR10101";
		}
		List<M002T002> faxinglist = pr101Business.listm002(box);

		PR10101From pr10101From = new PR10101From();
		pr10101From.setGukecode(gukecode);
		pr10101From.setNarudate(narudate);
		pr10101From.setNaruxian(naruxian);
		pr10101From.setLogontantoushaCode((String) request.getSession().getAttribute("logontantoushaCode"));
		pr10101From.setLogontantoushaMei((String) request.getSession().getAttribute("logontantoushaMei"));
		PR10101From resultform = new PR10101From();
		resultform = pr101Business.faxing(faxinglist, pr10101From);
		if ("".equals(resultform.getMsg())) {
			request.getSession().setAttribute("pr10101From", pr10101From);
			request.getSession().setAttribute("faxinglist", faxinglist);
			String str = "r = confirm('success');";
			request.setAttribute("str", str);
			zhuijiasholist.clear();

			List<M001> kokyakulist = pr101Business.kokyakulist("");
			for (int i = 0; i < kokyakulist.size(); i++) {
				kokyakulist.get(i).setStrshinyouGendoGaku(common.zifuchuan(kokyakulist.get(i).getStrshinyouGendoGaku()));
			}
			// 顧客リストを設定します
			request.setAttribute("kokyakulist", kokyakulist);

			List<M002> shouhinlist = pr101Business.shouhinlist("");
			for (int i = 0; i < shouhinlist.size(); i++) {
				shouhinlist.get(i).setStruriageTanka(common.zifuchuan(shouhinlist.get(i).getStruriageTanka()));
				shouhinlist.get(i).setStrzaikoSuu(common.zifuchuan(shouhinlist.get(i).getStrzaikoSuu()));
			}
			// 商品 リストを設定します
			request.setAttribute("shouhinlist", shouhinlist);
			String stsysdate = dfFormat.format(pr101Business.getNextday(sysdate));

			request.setAttribute("stsysdate", stsysdate);
			return "PR10101";

		} else {
			request.setAttribute("msg", pr10101From.getMsg());
			request.setAttribute("zhuijiasholist", zhuijiasholist);

			request.setAttribute("kokyakulist", pr101Business.kokyakulist(""));
			request.setAttribute("shouhinlist", pr101Business.shouhinlist(""));
			String stsysdate = dfFormat.format(pr101Business.getNextday(sysdate));
			request.setAttribute("gukecode", gukecode);
			request.setAttribute("stsysdate", stsysdate);
			return "PR10101";
		}
	}

	@RequestMapping("/zhangpiao.action")
	public void zhangpiao(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PR101Business pr101Business = new PR101BusinessImpl();

		List<M002T002> faList = (List<M002T002>) request.getSession().getAttribute("faxinglist");
		PR10101From pFrom = (PR10101From) request.getSession().getAttribute("pr10101From");
		Map<String, Object> map = pr101Business.test(pFrom, faList);

		ExcelUtils.exportToExcel(request, response, "受注商品明細.xls", "PR10101.xls", map);
	}
}
